package com.yogee.topseller.fragment.collect;


import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.adapter.CollectBodyOtherAdapter;
import com.yogee.topseller.bean.CollectOtherCompareBean;
import com.yogee.topseller.bean.CollectOtherPkBean;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.SearchResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 收藏后三个tab页面
 *
 * @author hanyu
 *         created by 2017/7/13 16:02
 */

public class CollectBodyOtherFragment extends HttpFragment implements RefreshUtil.OnRefreshListener {

    @BindView(R.id.body_other_recycler)
    EmptyRecyclerView bodyOtherRecycler;
    @BindView(R.id.body_refresh_other)
    TwinklingRefreshLayout bodyRefresh;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;

    Unbinder unbinder;
    private boolean isRefresh = false;//是否刷新
    int collectType = 0; //0.搜索，1.对比，2.PK
    CollectBodyOtherAdapter mAdapter;
    private List<CollectOtherPkBean.ProductList> pkList;//pk数据
    private List<CollectOtherCompareBean.ProductList> compareList;//对比数据
    private List<SearchResultBean.ResultBean> searchList;//对比数据

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect_body_other;
    }

    @Override
    protected void initView() {
        pkList = new ArrayList<>();
        compareList = new ArrayList<>();
        searchList = new ArrayList<>();
        iv_empty.setImageResource(R.drawable.nodata1);
        mAdapter = new CollectBodyOtherAdapter(getActivity(), getActivity());
        bodyOtherRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        bodyOtherRecycler.setAdapter(mAdapter);
        bodyOtherRecycler.setEmptyView(id_empty_view);
        MySinaRefreshView headerView = new MySinaRefreshView(getActivity());
        bodyRefresh.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        bodyRefresh.setHeaderView(headerView);
        bodyRefresh.setBottomView(new RefreshView(getActivity()));
        bodyRefresh.setBottomHeight(50);
        mAdapter.setCollectType(collectType);
    }

    @Override
    public void onResume() {
        super.onResume();
        isRefresh = true;
        if (null != getArguments()) {
            collectType = getArguments().getInt("collectType");
            if (0 == collectType) {
                searchRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (1 == collectType) {
                compareRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (2 == collectType) {
                pkRecord(AppUtil.getUserId(getActivity()), "0", "10");
            }
        }
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        if (AppUtil.isExamined(getActivity())) {
            if (0 == collectType) {
                searchRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (1 == collectType) {
                compareRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (2 == collectType) {
                pkRecord(AppUtil.getUserId(getActivity()), "0", "10");
            }
        }
    }
    //这是
    @Override
    public void onLoad() {
        isRefresh = false;
        if (AppUtil.isExamine(getActivity())) {
            if (0 == collectType) {
                searchRecord(AppUtil.getUserId(getActivity()), searchList.size() + "", "10");
            } else if (1 == collectType) {
                compareRecord(AppUtil.getUserId(getActivity()), compareList.size() + "", "10");
            } else if (2 == collectType) {
                pkRecord(AppUtil.getUserId(getActivity()), pkList.size() + "", "10");
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    /**
     * 获取搜索记录
     *
     * @param userId
     * @param total
     * @param count
     */
    private void searchRecord(String userId, String total, String count) {
        HttpManager.getInstance().searchRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SearchResultBean>() {
            @Override
            public void onNext(SearchResultBean o) {
                CollectBodyOtherFragment.this.loadingFinished();
                if (isRefresh) {
                    bodyRefresh.finishRefreshing();
                } else {
                    bodyRefresh.finishLoadmore();
                }
                if (null != o.getQueryList() && o.getQueryList().size() > 0) {
                    if (isRefresh) {
                        searchList.clear();
                        searchList = o.getQueryList();
                    } else {
                        searchList.addAll(o.getQueryList());
                    }
                    mAdapter.setCollectType(0);
                    mAdapter.setSearchList(searchList);
                }
            }
        }, this));
    }

    /**
     * 获取对比记录
     *
     * @param userId
     * @param total
     * @param count
     */
    private void compareRecord(String userId, String total, String count) {
        HttpManager.getInstance().comRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<CollectOtherCompareBean>() {
            @Override
            public void onNext(CollectOtherCompareBean o) {
                CollectBodyOtherFragment.this.loadingFinished();
                if (isRefresh) {
                    bodyRefresh.finishRefreshing();
                } else {
                    bodyRefresh.finishLoadmore();
                }
                if (null != o.getProductList() && o.getProductList().size() > 0) {
                    if (isRefresh) {
                        compareList.clear();
                        compareList = o.getProductList();
                    } else {
                        compareList.addAll(o.getProductList());
                    }
                    mAdapter.setCollectType(1);
                    mAdapter.setCompareList(compareList);
                }
            }
        }, this));
    }

    /**
     * 获取PK记录
     *
     * @param userId
     * @param total
     * @param count
     */
    private void pkRecord(String userId, String total, String count) {
        HttpManager.getInstance().pkRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<CollectOtherPkBean>() {
            @Override
            public void onNext(CollectOtherPkBean o) {
                CollectBodyOtherFragment.this.loadingFinished();
                if (isRefresh) {
                    bodyRefresh.finishRefreshing();
                } else {
                    bodyRefresh.finishLoadmore();
                }
                if (null != o.getProductList() && o.getProductList().size() > 0) {
                    if (isRefresh) {
                        pkList.clear();
                        pkList = o.getProductList();
                    } else {
                        pkList.addAll(o.getProductList());
                    }
                    mAdapter.setCollectType(2);
                    mAdapter.setPkData(pkList);
                }
            }
        }, this));
    }
}
